﻿using fenping.package;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace amuudp
{
    public partial class Form1 : Form
    {
        public SplitScreenServerEvents serverEvents;

        private ListViewItem currentSelectedViewItem;
        private Form2 fullForm;

        public Form1()
        {
            InitializeComponent();

            serverEvents = new SplitScreenServerEvents(1, 8111, this);
        }

        public void UpdatePicture(int clientId, Bitmap bitmap)
        {
            this.pictureBox1.Image = bitmap;
            if(fullForm != null)
            {
                fullForm.UpdateImage(bitmap);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //serverEvents.Connect("127.0.0.1", 8112);

            if(fullForm == null)
            {
                if(Screen.AllScreens.Length > 0)
                {
                    Screen screen = Screen.AllScreens[Screen.AllScreens.Length - 1];
                    fullForm = new Form2();
                    fullForm.Left = screen.Bounds.Left;
                    fullForm.UpdateImage(pictureBox1.Image);
                    fullForm.Show();
                }
            }
            else
            {
                fullForm.Close();
                fullForm.Dispose();
                fullForm = null;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
        }

        public void Log(object message)
        {
            this.label1.Text = message.ToString();
        }

        public void UpdateClientList(object obj)
        {
            List<ClientInfo> list = (List<ClientInfo>)obj;

            this.listView1.Items.Clear();
            foreach(ClientInfo item in list)
            {
                ListViewItem viewItem = new ListViewItem($"{item.Id}-{item.Name}-{item.Ip}-{item.Port}");
                viewItem.Tag = item;
                this.listView1.Items.Add(viewItem);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serverEvents.Stop();
            if(fullForm != null)
            {
                fullForm.Close();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.listView1.SelectedItems.Count == 1)
            {
                if (currentSelectedViewItem != null)
                {
                    ClientInfo oldClient = (ClientInfo)currentSelectedViewItem.Tag;
                    serverEvents.StopCapture(oldClient.Id);
                }
                ClientInfo client = (ClientInfo)this.listView1.SelectedItems[0].Tag;
                serverEvents.Capture(client.Id);
                currentSelectedViewItem = this.listView1.SelectedItems[0];
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
